// EBASLicensDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EBASLicens.h"
#include "EBASLicensDlg.h"
#include "rc4.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEBASLicensDlg dialog

CEBASLicensDlg::CEBASLicensDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEBASLicensDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEBASLicensDlg)
	m_eksamen = FALSE;
	m_faktura = FALSE;
	m_internet = FALSE;
	m_standard = FALSE;
	m_trial = FALSE;
	m_licensTil = _T("");
	m_slutDato = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEBASLicensDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEBASLicensDlg)
	DDX_Control(pDX, IDC_LIST_LICENSER, m_licenser);
	DDX_Check(pDX, IDC_CHECK_EKSAMEN, m_eksamen);
	DDX_Check(pDX, IDC_CHECK_FAKTURA, m_faktura);
	DDX_Check(pDX, IDC_CHECK_INTERNET, m_internet);
	DDX_Check(pDX, IDC_CHECK_STANDARD, m_standard);
	DDX_Check(pDX, IDC_CHECK_TRIAL, m_trial);
	DDX_Text(pDX, IDC_EDIT_TIL, m_licensTil);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_UDLOEB, m_slutDato);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEBASLicensDlg, CDialog)
	//{{AFX_MSG_MAP(CEBASLicensDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GEM, OnButtonGem)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEBASLicensDlg message handlers

BOOL CEBASLicensDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
		
	m_slutDato = m_slutDato.GetCurrentTime();
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEBASLicensDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEBASLicensDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEBASLicensDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEBASLicensDlg::OnOK() 
{
	UpdateData();

	CString info;
	CString type;
	CString key;
	CString dato;
/*
	CString til;
	m_licensTil.TrimRight(til);
	if ("" == til)
		return;
*/
	m_licenser.ResetContent();

	BYTE * pKey = NULL;
	long pKeyLen = 0;
	int nOfKeys = 0;
	
	if (TRUE == m_standard) {
		type = EBAS_STANDARD;

		dato = m_slutDato.Format("%Y-%m-%d");
		info = m_licensTil + type + dato;
		make_licens_key(info, &pKey, &pKeyLen);
		m_licenser.AddString(CString((char*)pKey) + "   " + type);
		free(pKey);
	}
	if (TRUE == m_internet) {
		type = EBAS_INTERNET;

		dato = m_slutDato.Format("%Y-%m-%d");
		info = m_licensTil + type + dato;
		make_licens_key(info, &pKey, &pKeyLen);
		m_licenser.AddString(CString((char*)pKey) + "   " + type);
		free(pKey);
	}
	if (TRUE == m_eksamen) {
		type = EBAS_EKSAMEN;

		dato = m_slutDato.Format("%Y-%m-%d");
		info = m_licensTil + type + dato;
		make_licens_key(info, &pKey, &pKeyLen);
		m_licenser.AddString(CString((char*)pKey) + "   " + type);
		free(pKey);
	}
	if (TRUE == m_faktura) {
		type = EBAS_FAKTURA;

		dato = m_slutDato.Format("%Y-%m-%d");
		info = m_licensTil + type + dato;
		make_licens_key(info, &pKey, &pKeyLen);
		m_licenser.AddString(CString((char*)pKey) + "   " + type);
		free(pKey);
	}
	if (TRUE == m_trial) {
		type = EBAS_TRIAL;

		dato = m_slutDato.Format("%Y-%m-%d");
		info = m_licensTil + type + dato;
		make_licens_key(info, &pKey, &pKeyLen);
		m_licenser.AddString(CString((char*)pKey) + "   " + type);
		free(pKey);
	}
	
	UpdateData(FALSE);
	
	return;
}


void CEBASLicensDlg::OnButtonGem() 
{
	static char BASED_CODE szFilter[] = "Tekst filer (*.txt)|*.txt|All Files (*.*)|*.*||";

	try {
		FILE * fp = NULL;
		CFileDialog * dialog = new CFileDialog(FALSE, "*.txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
		
		if (IDOK == dialog->DoModal()) {
			CString file = dialog->GetPathName();
			FILE * fp = fopen(file, "w");		
			if (!fp)
				return;

			for (int i = 0; i < m_licenser.GetCount(); i++) {				
				if (0 == i) {
					CString dato = m_slutDato.Format("%Y-%m-%d");
					fwrite("Udstedt til:          ", 1, strlen("Udstedt til:          "), fp);
					fwrite(m_licensTil, 1, strlen(m_licensTil), fp);
					fwrite("\n", 1, 1, fp);
					fwrite("Gyldig til:           ", 1, strlen("Gyldig til:           "), fp);
					fwrite(dato, 1, strlen(dato), fp);
					fwrite("\n", 1, 1, fp);
					fwrite("\n", 1, 1, fp);

					fwrite("Licens nr             Type", 1, strlen("Licens nr             Type"), fp);
					fwrite("\n", 1, 1, fp);
				}

				CString s;
				m_licenser.GetText(i, s);
				fwrite(s, 1, s.GetLength(), fp);
				fwrite("\n", 1, 1, fp);
			}
			
			if (fp)
				fclose(fp);
		}

	} catch (...) {
		MessageBox("Fejl ved lagring", "Information");
	}
}
